import 'dart:ffi';

import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/pages/goods/controller/comment_list_ctr.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:photo_view/photo_view.dart';
import 'package:video_player/video_player.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

import '../../../components/base/base_image.dart';
import '../../../components/base/base_nav_bar.dart';
import '../../../components/common/not_found.dart';
import '../../../components/decoration/decoration_video.dart';
import '../../../components/decoration/video_controls.dart';
import '../../../utils/Tools.dart';
import 'comment_page.dart';

///商品评价列表
class CommentListPage extends GetView<CommentListCtr> {
  const CommentListPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CommentListCtr>(builder: (c) {
      return BasePage(
        isShowPadding: false,
        enablePullDown: true,
        enablePullUp: controller.hasNextPage,
        refreshController: controller.baseRefreshCtr,
        onPageLoading: controller.onPageLoading,
        onPageRefresh: controller.onPageRefresh,
        title: '商品评价',
        iconType: IconType.back,
        children: [
          const SizedBox(
            height: 10,
          ),
          //订单状态筛选
          _Tabs(),
          controller.commentList != null
              ? Container(
                  margin: EdgeInsets.only(top: 10.h, bottom: 10.h),
                  child: ListView.builder(
                      itemCount: controller.commentList?.length,
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemBuilder: (context, index) {
                        var item = controller.commentList?[index];
                        //图片/视频
                        List commentFileList = item['commentFileList'] ?? [];
                        //回复
                        List commentReplyList = item['commentReplyList'] ?? [];
                        return Container(
                          margin: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                          padding: const EdgeInsets.only(top: 20, left: 10, right: 10, bottom: 10),
                          decoration: const BoxDecoration(
                            color: Colors.white,
                            // borderRadius: BorderRadius.all(Radius.circular(10)),
                          ),
                          child: Column(
                            children: [
                              Row(
                                children: [
                                  Expanded(
                                      child: Row(
                                    children: [
                                      SizedBox(child: BaseImage(url: item['userAvatar'], width: 30, height: 30, borderRadius: BorderRadius.circular(50 / 2))),
                                      Container(
                                        margin: const EdgeInsets.only(left: 10),
                                        alignment: Alignment.bottomLeft,
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.start,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          mainAxisSize: MainAxisSize.max,
                                          children: [
                                            Text(
                                              item['nickName'].toString(),
                                              style: TextStyle(color: Tools.fromHex("#999999")),
                                            ),
                                            starBox(item['channelStars'])
                                          ],
                                        ),
                                      )
                                    ],
                                  )),
                                  Container(
                                    alignment: Alignment.centerRight,
                                    child: Text(
                                      item['createTime'],
                                      style: TextStyle(color: Tools.fromHex("#999999")),
                                    ),
                                  )
                                ],
                              ),
                              if (item['commentText'].toString() != '') ...[
                                Container(
                                  margin: const EdgeInsets.only(top: 10),
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    item['commentText'].toString(),
                                    style: TextStyle(color: Tools.fromHex("#333333")),
                                  ),
                                ),
                              ],
                              Container(
                                padding: const EdgeInsets.only(left: 18, right: 18, top: 18, bottom: 0),
                                alignment: Alignment.topLeft,
                                child: Wrap(spacing: 10.w, runSpacing: 10.w, direction: Axis.horizontal, children: [
                                  for (var item in commentFileList)
                                    Container(
                                      child: GestureDetector(
                                        onTap: () {
                                          showDialog(
                                              context: context,
                                              builder: (c) {
                                                bool isImg = item['type'] == 1;
                                                if (!isImg) {
                                                  controller.initVideoC(item['url']);
                                                }
                                                return Dialog(
                                                  child: isImg
                                                      ? PhotoView(
                                                          imageProvider: NetworkImage(item['url']),
                                                        )
                                                      :
                                                      // DecorationVideo(info: {'selectUrl':item['url']});
                                                      AspectRatio(
                                                          aspectRatio: controller.videoController!.value.aspectRatio,
                                                          child: Stack(
                                                            alignment: Alignment.bottomCenter,
                                                            children: [
                                                              VideoPlayer(controller!.videoController!),
                                                              // VideoControls(
                                                              //   controller:
                                                              //       controller!
                                                              //           .videoController!,
                                                              //   info: {
                                                              //     'surfacePlotImage':
                                                              //         ''
                                                              //   },
                                                              // ),
                                                              VideoProgressIndicator(controller!.videoController!, allowScrubbing: true),
                                                            ],
                                                          ),
                                                        ),
                                                );
                                              });
                                        },
                                        child: SizedBox(
                                          child: Container(
                                              color: Colors.grey,
                                              width: 95.w,
                                              height: 95.w,
                                              child: BaseImage(
                                                url: item['url'],
                                                width: 200.w,
                                                height: 200.w,
                                                borderRadius: BorderRadius.circular(10),
                                              )),
                                        ),
                                      ),
                                    )
                                ]
                                    // List.generate(controller.imageList!.length, (index){
                                    //   return ImageItem(index: index);
                                    // }),
                                    ),
                              ),
                              commentReplyList.isNotEmpty
                                  ? Container(
                                      decoration: BoxDecoration(
                                        color: Tools.fromHex('#f8f8f8'),
                                        borderRadius: const BorderRadius.all(Radius.circular(10)),
                                      ),
                                      alignment: Alignment.centerLeft,
                                      child: Container(
                                        margin: EdgeInsets.only(bottom: 10.h),
                                        // color: Colors.red,
                                        child: Wrap(
                                          alignment: WrapAlignment.start,
                                          crossAxisAlignment: WrapCrossAlignment.start,
                                          spacing: 10.w,
                                          runSpacing: 10.w,
                                          direction: Axis.horizontal,
                                          children: [for (var item in commentReplyList) replyContentStyle(item)],
                                        ),
                                      ),
                                    )
                                  : const SizedBox(),
                            ],
                          ),
                        );
                      }
                      // )
                      ),
                )
              : const NotFound(),
        ],
      );
    });
  }

  Widget replyContentStyle(Map item) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: ClipOval(
                child: Image.network(
                  item['userAvatar'],
                  height: 30,
                  width: 30,
                ),
              ),
            ),
            const SizedBox(
              width: 5,
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  item['nickName'],
                  style: TextStyle(fontSize: 12, color: Tools.fromHex('#999999')),
                ),
                Text(
                  item['createTime'],
                  style: TextStyle(fontSize: 12, color: Tools.fromHex('#999999')),
                ),
              ],
            )
          ],
        ),
        Padding(
          padding: const EdgeInsets.only(left: 20, top: 0),
          child: Text(
            item['comment'],
            style: TextStyle(fontSize: 13, color: Tools.fromHex('#333333')),
          ),
        ),
      ],
    );
  }

  Widget _Tabs() => SizedBox(
        height: 55,
        width: double.infinity,
        child: TabBar(
            dividerColor: Colors.white,
            indicator: const BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 2, color: Colors.red),
              ),
            ),
            labelColor: Colors.black,
            isScrollable: true,
            unselectedLabelColor: Colors.black,
            controller: controller.statusTabController,
            tabAlignment: TabAlignment.center,
            labelStyle: const TextStyle(
              fontSize: 16.0, // 设置选中的字体大小
              fontWeight: FontWeight.bold, // 设置选中的字体粗细
            ),
            unselectedLabelStyle: const TextStyle(
              fontSize: 14.0, // 设置未选中的字体大小
              fontWeight: FontWeight.normal, // 设置未选中的字体粗细
            ),
            tabs: [
              Column(
                children: [
                  const Text('全部'),
                  Container(
                    margin: const EdgeInsets.only(top: 5),
                    child: Text(
                      '',
                      style: TextStyle(color: Tools.fromHex("#999999")),
                    ),
                  ),
                ],
              ),
              Column(
                children: [
                  const Text('图/视频'),
                  Container(
                    margin: const EdgeInsets.only(top: 5),
                    child: Text(
                      '${controller.data?['fileReviewNumber'].toString()}',
                      style: TextStyle(color: Tools.fromHex("#999999")),
                    ),
                  ),
                ],
              ),
              Column(
                children: [
                  const Text('好评'),
                  Container(
                    margin: const EdgeInsets.only(top: 5),
                    child: Text(
                      '${controller.data?['positiveReviewNumber'].toString()}',
                      style: TextStyle(color: Tools.fromHex("#999999")),
                    ),
                  ),
                ],
              ),
              Column(
                children: [
                  const Text('中评'),
                  Container(
                    margin: const EdgeInsets.only(top: 5),
                    child: Text(
                      '${controller.data?['middleReviewNumber'].toString()}',
                      style: TextStyle(color: Tools.fromHex("#999999")),
                    ),
                  ),
                ],
              ),
              Column(
                children: [
                  const Text('差评'),
                  Container(
                    margin: const EdgeInsets.only(top: 5),
                    child: Text(
                      '${controller.data?['badReviewNumber'].toString()}',
                      style: TextStyle(color: Tools.fromHex("#999999")),
                    ),
                  ),
                ],
              ),
            ]),
      );

  ///星星组件
  Widget starBox(int value) {
    return RatingBar.builder(
      itemSize: 20,
      ignoreGestures: true,
      initialRating: double.parse(value.toString()),
      minRating: 1,
      direction: Axis.horizontal,
      allowHalfRating: false,
      itemCount: 5,
      // itemPadding: EdgeInsets.symmetric(horizontal: 1.0.w),
      itemBuilder: (context, _) => Icon(
        Icons.star,
        color: Tools.fromHex("#F93B19"),
      ),
      onRatingUpdate: (v) {},
    );
  }
}
